package com.uppfind.framework.utils.sql;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 将数据库sql colum转换为驼峰式属性
 * Created by xuliugen on 2017/7/1.
 */
public class SqlCamelFiledTranslateUtil {

    public static final char DEFAULT_SQL_UNDERLINE = '_';
    public static final String DEFAULT_SQL_COMMA = ",";
    public static final String DEFAULT_SQL_SPACE = " ";
    public static final String DEFAULT_SQL_SUFFIX = DEFAULT_SQL_COMMA + DEFAULT_SQL_SPACE;


    public static String camelToUnderline(Set<String> param) {
        StringBuffer sb = new StringBuffer(DEFAULT_SQL_SPACE);

        param.forEach(filed -> {
            sb.append(camelToUnderline(filed)).append(DEFAULT_SQL_SUFFIX);
        });

        //示例：" u_id, update_date, phone, name, email, username, status, create_date " 前后都有空格
        return sb.toString().substring(0, sb.lastIndexOf(DEFAULT_SQL_SUFFIX)) + DEFAULT_SQL_SPACE;
    }

    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuffer sb = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(DEFAULT_SQL_UNDERLINE);
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static Set<String> underlineToCamel(String[] params) {
        Set<String> result = new HashSet<>(params.length);
        for (int i = 0; i < params.length; i++) {
            result.add(underlineToCamel(params[i].trim()));
        }
        return result;
    }

    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuffer sb = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == DEFAULT_SQL_UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String underlineToCamel2(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        StringBuffer sb = new StringBuffer(param);
        Matcher mc = Pattern.compile("_").matcher(param);
        int i = 0;
        while (mc.find()) {
            int position = mc.end() - (i++);
            sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String sqlField = "u_id,  username,   password,    name, email, phone, status, create_date, update_date";
        String[] sqlFields = sqlField.trim().split(",");
        for (String field : sqlFields) {
            System.out.println(underlineToCamel(field.trim()));
        }

    }
}
